from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage, InvalidPage
from django.db.models import Q
from django.http import JsonResponse
from django.shortcuts import render, redirect
from django.urls import reverse
from taggit.models import Tag
from comment.models import Comment

from blog import models
from .forms.blog import BlogPageModelForm


# Create your views here.

def blog_index(request, tag_slug=None):
    if request.method == 'GET':
        # form = BlogUpdateModelForm()
        data = models.BlogPage.objects.filter(
            Q(is_public=1) | Q(is_public=2, author=request.universe.user) | Q(is_public=3,
                                                                              author=request.universe.user)).all().order_by(
            '-create_datetime')
        if tag_slug:
            pass
            # tag = get_object_or_404(Tag, slug = tag_slug)
            # data = data.filter(tags__in=[tag])

        paginator = Paginator(data, 4)
        page = request.GET.get('page')
        try:
            data = paginator.page(page)
        except PageNotAnInteger:
            data = paginator.page(1)
        except(EmptyPage, InvalidPage):
            data = paginator.page(paginator.num_pages)

        return render(request, 'blog/blog_index.html', {'data': data})


def blog_post(request, blogpage_id):
    if request.method == 'GET':
        post_object = models.BlogPage.objects.filter(id=blogpage_id).first()
        # 取出文章评论
        comments = Comment.objects.filter(post=blogpage_id)

        return render(request, 'blog/blog_page.html', {'post_object': post_object, 'comments': comments})


def blog_add(request):
    if request.method == 'GET':
        form = BlogPageModelForm(request)

        return render(request, 'blog/blog_page_form.html', {'form': form})

    form = BlogPageModelForm(request, data=request.POST)
    if form.is_valid():
        form.instance.author = request.universe.user
        form.save()
        return redirect('blog_index')

    return JsonResponse({'status': False, 'error': form.errors})


def blog_edit(request, blogpage_id):
    """ 编辑文章 """
    # type = 'edit'
    blog_object = models.BlogPage.objects.filter(id=blogpage_id).first()
    # print(wiki_object.content)
    if not blog_object:
        url = reverse('blog_post', kwargs={'blogpage_id': blogpage_id})
        return redirect(url)

    if request.method == "GET":
        form = BlogPageModelForm(request, instance=blog_object)
        return render(request, 'blog/blog_page_form.html', {'form': form})

    form = BlogPageModelForm(request, data=request.POST, instance=blog_object)
    if form.is_valid():
        # if form.instance.parent:
        #     form.instance.depth = form.instance.parent.depth + 1
        # else:
        #     form.instance.depth = 1
        form.save()
        url = reverse('blog_post', kwargs={'blogpage_id': blogpage_id})
        # preview_url = "{0}?page={1}".format(url, blogpage_id)
        return redirect(url)

    return render(request, 'blog/blog_page_form.html', {'form': form})


def blog_tag(request):
    pass
